package com.lp.biz.medal.component;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lp.biz.identity.model.entity.BizIdentity;
import com.lp.biz.medal.mapper.MedalMapper;
import com.lp.biz.medal.mapper.MedalMemberRelMapper;
import com.lp.biz.medal.model.entity.Medal;
import com.lp.biz.medal.model.entity.MedalMemberRel;
import com.lp.common.constant.IdentityConstant;
import com.lp.common.constant.MedalConstant;
import com.lp.common.exception.BaseException;
import com.lp.common.exception.ErrorCodeEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;

import static com.lp.common.constant.MedalConstant.*;

/**
 * @program: backend
 * @description:
 * @author: Ke.Song
 * @since: 2024-05-11 18:17:30
 */
@Slf4j
@Component
public class MedalFactory {

    @Autowired
    private MedalMapper medalMapper;

    @Autowired
    private MedalMemberRelMapper relMapper;

    private static Map<String, Medal> medalMaps;

    private void selectMedal(String code, Integer memberId) {
        if (medalMaps == null || medalMaps.isEmpty()) {
           initMap();
        }
        Medal medal = medalMaps.get(code);
        if (medal == null) {
            throw new BaseException(ErrorCodeEnum.PARAMS_ERROR);
        }
        MedalMemberRel rel = new MedalMemberRel();
        rel.setMemberId(memberId);
        rel.setCreateAt(LocalDateTime.now());
        if (code.equals(ACTION_POWER)) {
            rel.setMedalId(medal.getId());
        } else if (code.equals(READING_EXPERTS)) {
            rel.setMedalId(medal.getId());
        } else if (code.equals(WRITING_EXPERTS)) {
            rel.setMedalId(medal.getId());
        } else if (code.equals(STORYTELLING_EXPERTS)) {
            rel.setMedalId(medal.getId());
        } else if (code.equals(TEACH_EXPERTS)) {
            rel.setMedalId(medal.getId());
        } else if (code.equals(READING_ASSISTANT)) {
            rel.setMedalId(medal.getId());
        } else if (code.equals(READING_MENTOR)) {
            rel.setMedalId(medal.getId());
        } else if (code.equals(READING_TRAINER)) {
            rel.setMedalId(medal.getId());
        } else if (code.equals(WRITING_ASSISTANT)) {
            rel.setMedalId(medal.getId());
        } else if (code.equals(WRITING_MENTOR)) {
            rel.setMedalId(medal.getId());
        } else if (code.equals(WRITING_TRAINER)) {
            rel.setMedalId(medal.getId());
        } else if (code.equals(SPEECH_ASSISTANT)) {
            rel.setMedalId(medal.getId());
        } else if (code.equals(SPEECH_MENTOR)) {
            rel.setMedalId(medal.getId());
        } else if (code.equals(SPEECH_TRAINER)) {
            rel.setMedalId(medal.getId());
        } else if (code.equals(VOICE_ASSISTANT)) {
            rel.setMedalId(medal.getId());
        } else if (code.equals(VOICE_TRAINER)) {
            rel.setMedalId(medal.getId());
        } else {
            throw new BaseException(ErrorCodeEnum.PARAMS_ERROR);
        }
        MedalMemberRel one = relMapper.selectOne(new QueryWrapper<MedalMemberRel>().lambda()
                .eq(MedalMemberRel::getMemberId, memberId)
                .eq(MedalMemberRel::getMedalId, rel.getMedalId()));
        if (one == null) {
            log.info("勋章：点亮成功，用户：{}，勋章：{}", memberId, code);
            relMapper.insert(rel);
        }
    }

    public void initMap() {
        medalMaps = new HashMap<>(6);
        for (Medal medal : medalMapper.selectList(new QueryWrapper<>())) {
            medalMaps.put(medal.getCode(), medal);
        }
    }

    @Transactional(rollbackFor = RuntimeException.class)
    public void addMedal(Integer memberId, BizIdentity identity) {
        String code = identity.getCode();
        String medalCode = switch (code) {
            case IdentityConstant.READING_ASSISTANT -> MedalConstant.READING_ASSISTANT;
            case IdentityConstant.READING_MENTOR -> MedalConstant.READING_MENTOR;
            case IdentityConstant.READING_TRAINER -> MedalConstant.READING_TRAINER;
            case IdentityConstant.WRITING_ASSISTANT -> MedalConstant.WRITING_ASSISTANT;
            case IdentityConstant.WRITING_MENTOR -> MedalConstant.WRITING_MENTOR;
            case IdentityConstant.WRITING_TRAINER -> MedalConstant.WRITING_TRAINER;
            case IdentityConstant.SPEECH_ASSISTANT -> MedalConstant.SPEECH_ASSISTANT;
            case IdentityConstant.SPEECH_MENTOR -> MedalConstant.SPEECH_MENTOR;
            case IdentityConstant.SPEECH_TRAINER -> MedalConstant.SPEECH_TRAINER;
            case IdentityConstant.VOICE_ASSISTANT -> MedalConstant.VOICE_ASSISTANT;
            case IdentityConstant.VOICE_TRAINER -> MedalConstant.VOICE_TRAINER;
            default -> null;
        };
        if (medalCode != null) {
            selectMedal(medalCode, memberId);
        }
    }
}
